﻿namespace EFS.RC.Presences.Controls.Lync.Presences
{
	/// <summary>
	/// Interaction logic for List.xaml
	/// </summary>
	public partial class ListVertical : EFS.RC.Presences.Controls.Lync.Presences.List
	{
        // all rows
        private System.Collections.Generic.Dictionary<System.String, System.Windows.Controls.RowDefinition> gridRowDefinitions = new System.Collections.Generic.Dictionary<System.String, System.Windows.Controls.RowDefinition>();
        private System.Collections.Generic.Dictionary<System.String, EFS.RC.Presences.Controls.Lync.Presences.Presence> gridPresences = new System.Collections.Generic.Dictionary<System.String, EFS.RC.Presences.Controls.Lync.Presences.Presence>();

		public ListVertical()
		{
			InitializeComponent();
        }

        /// <summary>
        /// Load presences
        /// </summary>
        /// <param name="sipArray"></param>
        public override void LoadPresences(System.String[] sipArray)
        {
            // new sips
            System.Collections.Generic.List<System.String> sipList = new System.Collections.Generic.List<System.String>(sipArray);
            System.Collections.Generic.List<System.String> newSipList = new System.Collections.Generic.List<System.String>();
            System.Collections.Generic.List<System.String> removeSipList = new System.Collections.Generic.List<System.String>();

            // add new 
            foreach (System.String sip in sipArray)
            {
                // check
                if (!gridRowDefinitions.ContainsKey(sip))
                {
                    // add
                    newSipList.Add(sip);
                }
            }

            // add remove
            foreach (System.String sip in gridRowDefinitions.Keys)
            {
                // check
                if (!sipList.Contains(sip))
                {
                    // add
                    removeSipList.Add(sip);
                }
            }

            // remove
            foreach (System.String sip in removeSipList)
            {
                // remove
                gridControlPresences.RowDefinitions.Remove(gridRowDefinitions[sip]);
                gridControlPresences.Children.Remove(gridPresences[sip]);
            }

            // add
            foreach (System.String sip in newSipList)
            {
                // add head row definition
                System.Windows.Controls.RowDefinition rowDef = new System.Windows.Controls.RowDefinition();
                rowDef.Height = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
                gridControlPresences.RowDefinitions.Add(rowDef);
                gridRowDefinitions[sip] = rowDef;

                // presence
                EFS.RC.Presences.Controls.Lync.Presences.Presence controlPresence = new EFS.RC.Presences.Controls.Lync.Presences.Presence();
                controlPresence.SIP = sip;
                controlPresence.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                controlPresence.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                gridControlPresences.Children.Add(controlPresence);
                gridPresences[sip] = controlPresence;

                // set position
                System.Windows.Controls.Grid.SetRow(controlPresence, gridControlPresences.RowDefinitions.Count - 1);
                System.Windows.Controls.Grid.SetColumn(controlPresence, 0);
            }
        }
	}

}